﻿using NewRen.Controllers;
using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using NewRen.Models.ViewModel;
using NewRen.Schemas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace NewRen.Areas.Admin.Controllers
{
    [RouteArea("Admin")]
    public class UserController : BaseController
    {
        // GET: Admin/User
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [Route("Login")]
        public ActionResult Login()
        {
            if (User.Identity.IsAuthenticated)
            {
                return Redirect("/Admin/Home/Index");
            }
            return View();
        }

        [HttpPost]
        [Route("Login")]
        public ActionResult Login(vAdmin model)
        {
            if (ModelState.IsValid)
            {
                Models.Admin user = new Models.Admin();
                model.Password = Helpers.Encryt.GetMD5(model.Password);
                user = db.Admins.Where(u => u.UserName == model.Username && u.Password == model.Password).SingleOrDefault();
                if (user == null)
                {
                    ModelState.AddModelError("", "用户名或密码错误！");
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(model.Username, true);
                    log.Info(new LogContent(user.UserName + "-管理员登录", Helpers.HttpHelper.GetIPAddress()));
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "登陆信息错误请重新填写！");
            }
            return View(model);
        }



        #region 注销
        /// <summary>
        ///  注销 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ValidateSID]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login");
        }
        #endregion

        /// <summary>
        /// 个人信息展示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Schema.LoginCheck]
        public ActionResult Show(int id)
        {
            var admin = db.Admins.Find(id);
            return View(admin);
        }

        #region 修改个人信息
        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Schema.LoginCheck]
        public ActionResult Edit(int id)
        {
            var admin = db.Admins.Find(id);
            return View(admin);
        }

        [Schema.LoginCheck]
        [HttpPost]
        public ActionResult Edit(Models.Admin model)
        {
            var admin = db.Admins.Find(model.ID);
            if (ModelState.IsValid)
            {
                try
                {
                    admin.RealName = model.RealName;
                    admin.Phone = model.Phone;
                    admin.QQ = model.QQ;
                    admin.Email = model.Email;
                    admin.Address = model.Address;
                    db.SaveChanges();
                    return Redirect("/Admin/System/Index");
                }
                catch
                {
                    ModelState.AddModelError("", "个人信息修改失败！");
                }
            }
            else
            {
                ModelState.AddModelError("", "信息填写错误！");
            }
            return View(admin);
        }

        #endregion

        [HttpGet]
        public ActionResult ShowPicture(int id)
        {
            Models.Admin admin = new Models.Admin();
            admin = db.Admins.Find(id);
            return File(admin.Picture, "image/jpg");
        }

       

    }
}